package liming.tool.lock;

import java.util.concurrent.atomic.AtomicBoolean;

public final class LockAtomicBoolean {
    private final CodeLock codeLock;
    private final AtomicBoolean value;
    public LockAtomicBoolean(CodeLock codeLock) {
        this.codeLock = codeLock;
        value = new AtomicBoolean(false);
    }
    public boolean get() {
        return codeLock.lock(value::get);
    }
    public boolean set(boolean value){
        return codeLock.lock(()->{
            boolean old = this.value.get();
            this.value.set(value);
            return old != value;
        });
    }
    @Override
    public String toString() {
        return String.valueOf(value);
    }
}
